/**
* parsing class for ex10, parses an expression
* @author Daniel Shalev
*/
public class Parser{
	
	/**
    * 
    */
    public Parser() {
    	
    }
    /**
    * Parses a factor. A Factor can be one of the following: 1) number; 2) variable;
    *  3) expression in brackets.
    * @param scan the Tokenizer
    * @return Number object if the next element is number. 
    * Variabe object if the next element is variable.  
    * In case of expression in brackets it calls parseExpression
    *  for the expression in brackets and returns the result. 
    * Returns null otherwise.
    */
    public static Expression parseFactor(Tokenizer scan) {
    	Expression reExpress=null;
    	//checks if the next element is a variable, number or a bracket
    	//and assigns the needed value to the returned expression
    	if(scan.peek()==ElementType.VARIABLE){
    		reExpress=new Variable(scan.nextElement());
    	}
    	else if(scan.peek()==ElementType.NUMBER){
    		reExpress=new Number(Double.parseDouble(scan.nextElement()));
    	}
    	else if(scan.peek()==ElementType.BRACKET){
    		scan.nextElement();
    		reExpress=parseExpression(scan);
    	}
    	return reExpress;
    }
    /**
    * Parses an expression. An expression can be Term { [+/-] Term }.
    * Calls parseTerm to get the first expression. 
    * If operations "+" or "-" are followed, it calls ParseTerm again to get the second expression
    * and creates an Operand object with the operand acts on the two obtained expressions. 
    * The process is repeated for every addition "+" or "-" operation.
    * @param scan the Tokenizer
    * @return the result of parseTerm if only one operand exist.
    *  The operation result if more than one operand exist.
    */
    public static Expression parseExpression(Tokenizer scan) {
    	Expression reExpress=parseTerm(scan);
    	
    	while(scan.peek()==ElementType.PLUS||scan.peek()==ElementType.MINUS){
    		//assigns the operator (plus or minus) to a string
    		String operator=scan.nextElement();
    		//assigns the second expression
    		Expression secondExpress=parseTerm(scan);
    		reExpress=new Operator(operator,reExpress,secondExpress);
    	}
    	//if the next element is a bracket go to next one 
    	if(scan.peek()==ElementType.BRACKET)
    		scan.nextElement();
    	return reExpress;
    }
    /**
    * Parses a term. A term can be Factor { [%/*]  Factor }.
    * Calls parseFactor to get the first expression.
    * If operations "*" or "/" are followed, it calls ParseFactor again to get the second expression 
    * and creates an Operand object with the operand acts on the two obtained expressions.
    * The process is repeated for every addition "*" or "/" operation.
    * @param scan the Tokenizer
    * @return the result of parseFactor if only one operand exist.
    *  The operation result if more than one operand exist.
    */
    public static Expression parseTerm(Tokenizer scan) {
    	Expression reExpress=parseFactor(scan);
        
    	while(scan.peek()==ElementType.DIV||scan.peek()==ElementType.MUL){
    		//assigns the operator (div or mul) to a string
    		String operator=scan.nextElement();
    		//assigns the second expression
    		Expression secondExpress=parseFactor(scan);
    		reExpress=new Operator(operator,reExpress,secondExpress);
    	}
    	return reExpress;
    }
}